package com.deve.pig.controller.merchant;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import my.utils.CommonUtil;
import my.utils.DwzAjaxJsonUtil;
import my.utils.Pagination;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.deve.pig.controller.BaseController;
import com.deve.pig.model.MerchantBranchStore;
import com.deve.pig.service.merchant.IMerchantBranchStoreService;

/**
 * @description 分店控制器
 * @author tanyouzhong
 * @date 2014年9月13日 下午6:31:21
 */
@Controller
@RequestMapping("/merchant/store")
public class MerchantStoreController extends BaseController
{

	@Resource
	private IMerchantBranchStoreService merchantStoreService;

	@RequestMapping("/showList")
	public String shownList(Model model, HttpServletRequest request, HttpServletResponse response)
	{

		Map<String, Object> params = super.getRequestParameters(request);
		int pageNum = Pagination.CURRENTPAGE;
		int pageSize = Pagination.PAGESIZE;
		if (params.containsKey("pageNum"))
		{
			pageNum = Integer.parseInt((String) params.get("pageNum"));
		}
		if (params.containsKey("numPerPage"))
		{
			pageSize = Integer.parseInt((String) params.get("numPerPage"));
		}
		Pagination page = merchantStoreService.findPagination(params, pageNum, pageSize);

		model.addAttribute("page", page);

		return "admin/merchant/store/storelist";
	}

	@RequestMapping("/showAdd")
	public String showAdd(Model model)
	{
		return "admin/merchant/store/add";
	}

	@RequestMapping("/add")
	@ResponseBody
	public Map<String, Object> add(MerchantBranchStore store, HttpServletRequest request) throws IOException
	{
		Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
		json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
		if (store == null)
		{
			json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
			json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
		}
		else
		{
			if (merchantStoreService.add(store) < 1)
			{
				json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
				json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "抱歉，服务器故障，请重试");
			}
		}
		return json;
	}

	@RequestMapping("/delete/{id}")
	@ResponseBody
	public Map<String, Object> delete(@PathVariable("id") int id, HttpServletRequest request) throws IOException
	{
		Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
		json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
		json.put(DwzAjaxJsonUtil.KEY_CALLBACKTYPE, "");

		if (merchantStoreService.delete(id) < 1)
		{
			json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
			json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "删除失败！");
		}
		return json;
	}

	@RequestMapping("/showUpdate/{id}")
	public String showUpdate(@PathVariable("id") int id, Model model)
	{
		MerchantBranchStore store = merchantStoreService.findById(id);
		model.addAttribute("store", store);
		return "admin/merchant/store/update";
	}

	@RequestMapping("/update")
	@ResponseBody
	public Map<String, Object> update(MerchantBranchStore store, HttpServletRequest request) throws IOException
	{
		Map<String, Object> json = DwzAjaxJsonUtil.getDefaultAjaxJson();
		json.put(DwzAjaxJsonUtil.KEY_NAVTABID, getNavTabId(request));
		if (store == null)
		{
			json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
			json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "操作失败！");
		}
		else
		{
			if (merchantStoreService.update(store) < 1)
			{
				json.put(DwzAjaxJsonUtil.KEY_STATUSCODE, 300);
				json.put(DwzAjaxJsonUtil.KEY_MESSAGE, "抱歉，服务器故障，请重试");
			}
		}
		return json;
	}

	@RequestMapping("/parseAddr")
	@ResponseBody
	public Map<String, Object> parseAddr(@RequestParam(value = "address") String address, HttpServletRequest request)
			throws IOException
	{
		Map<String, Object> json = new HashMap<String, Object>();

		final String url = CommonUtil.BAIDU_HEADER + "?address=" + address + "&output=json&ak=" + CommonUtil.BAIDU_AK;
		try
		{
			String result = CommonUtil.getJsonString(url);
			JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(result);
			JSONObject locationJson = (JSONObject) JSONSerializer.toJSON(jsonObject.get("result"));
			JSONObject lngJson = (JSONObject) JSONSerializer.toJSON(locationJson.get("location"));
			json.put("success", true);
			json.put("lng", lngJson.get("lng"));
			json.put("lat", lngJson.get("lat"));
		}
		catch (UnsupportedEncodingException e)
		{
			e.printStackTrace();
			json.put("success", false);
		}
		catch (Exception e)
		{
			e.printStackTrace();
			json.put("success", false);
		}
		return json;
	}
}
